package com.william.jdbcplus.boot.test.util;

import com.william.jdbcplus.boot.test.entity.UserEntity;
import com.william.jdbcplus.boot.test.entity.TenantEntity;
import com.william.jdbcplus.core.query.QueryLambdaWrap;

import java.util.Arrays;

public class QueryLambdaUtil {
    public static void main(String[] args) {
        QueryLambdaWrap lambdaWrap = new QueryLambdaWrap();
        lambdaWrap
                .selectMax(UserEntity::getAccount)
                .select(TenantEntity::getTenantId)
                .selectAs(UserEntity::getAccount, TenantEntity::getTenantId)
                .from(TenantEntity.class)
                .leftJoin(UserEntity.class, UserEntity::getTenantId, TenantEntity::getTenantId)
                .eq(UserEntity::getAccount, "admin")
                .or(i -> i.gt(UserEntity::getOrgCode, 1)
                        .like(UserEntity::getOrgCode, "admin")
                )
                .not().between(TenantEntity::getCreateTime, "2020-01-02 05:04:03", "2020-01-05 05:04:09")
                .or().eq(TenantEntity::getIsDeleted, 0)
                .not(i -> i.in(UserEntity::getUserName, Arrays.asList("admin", "ni", "ki")))
                .notLike(UserEntity::getAccount, "123")
                .between(UserEntity::getClientId, 1, 2)
                .notExists(i -> i.select(UserEntity::getClientId)
                        .from(UserEntity.class).eq(UserEntity::getClientId, 1)
                )
                .groupBy(UserEntity::getAccount)
                .connect("1=0")
                .havingMax(i -> i.eq(UserEntity::getAccount, "admin").or().gt(TenantEntity::getStatus, 2))
                .having(i -> i
                        .gt(UserEntity::getOrgCode, 2)
                        .like(UserEntity::getOrgCode, "ad")
                )
                .orderBy(true, UserEntity::getAccount)
                .union(i -> i
                        .select(UserEntity::getUserName)
                        .from(UserEntity.class)
                        .eq(UserEntity::getUserName, "niliwei")
                );

        String sql = lambdaWrap.getExecSql();
        System.out.println(sql);

        //        select max(user_entity.account),tenant_entity.tenant_id,user_entity.account as tenantId_
        //        from tenant_entity
        //        left join user_entity on user_entity.tenant_id = tenant_entity.tenant_id
        //        where user_entity.account = 'admin'
        //        or (user_entity.org_code > 1 and lower(user_entity.org_code) like lower('%admin%'))
        //        and not tenant_entity.create_time between '2020-01-02 05:04:03' and '2020-01-05 05:04:09'
        //        or tenant_entity.is_deleted = 0
        //        and not (user_entity.user_name in ('admin','ni','ki'))
        //        and lower(user_entity.account) not like lower('%123%')
        //        and user_entity.client_id between 1 and 2
        //        and not exists
        //                (select user_entity.client_id from user_entity where user_entity.client_id = 1)
        //        and 1=0
        //        group by user_entity.account
        //        having max(user_entity.account) = 'admin' or max(tenant_entity.status) > 2
        //        and user_entity.org_code > 2 and lower(user_entity.org_code) like lower('%ad%')
        //        order by user_entity.account asc
        //        union
        //        select user_entity.user_name
        //        from user_entity
        //        where user_entity.user_name = 'niliwei'

    }
}
